<template>
    <div class="queryOrg">
        <Row>
            <Col class="query-form">
                <Button size="large" class="ivu-btn-add" @click="routeTo" >返回</Button>
                <Button size="large" icon="md-add" class="ivu-btn-add" @click='drawershow(3)'>新增</Button>
            </Col>
        </Row>
        <Tree :data="codeTree" :load-data="loadData" @on-select-change="treechange" style="font-size: 29px;"></Tree>
        <Drawer :width="60" :title="drawertitle==1?'编码明细查询':drawertitle==2?'编码明细修改':'编码明细新增'"  :closable="false" v-model="value1">
            <Form ref="formCustom" :model="formCustom" :rules="ruleCustom" :label-width="160" style="padding: 20px;">
                <Row>
                    <Col span="12">
                        <FormItem label="编码项中文名称：" prop="codingName">
                            <div v-if="drawertitle==1">{{formCustom.codingName}}</div>
                            <Input type="text" v-model="formCustom.codingName" v-else></Input>
                        </FormItem>
                    </Col>
                    <Col span="12">
                        <FormItem label="编码项英文名称：" prop="codingEnName">
                            <div v-if="drawertitle==1">{{formCustom.codingEnName}}</div>
                            <Input type="text" v-model="formCustom.codingEnName" v-else></Input>
                        </formitem>
                    </Col>
                </Row>
                <Row>
                    <Col span="12">
                        <FormItem label="编码项俄文名称： " prop="codingRuName">
                            <div v-if="drawertitle==1">{{formCustom.codingRuName}}</div>
                            <Input type="text" v-model="formCustom.codingRuName" v-else></Input>
                        </FormItem>
                    </Col>
                    <Col span="12">
                        <FormItem label="编码项法文名称：" prop="codingFrName">
                            <div v-if="drawertitle==1">{{formCustom.codingFrName}}</div>
                            <Input type="text" v-model="formCustom.codingFrName" v-else></Input>
                        </formitem>
                    </Col>
                </Row>
                <Row>
                    <Col span="12">
                        <FormItem label="编码项西班牙文名称：" prop="codingEsName">
                            <div v-if="drawertitle==1">{{formCustom.codingEsName}}</div>
                            <Input type="text" v-model="formCustom.codingEsName" v-else></Input>
                        </FormItem>
                    </Col>
                    <Col span="12">
                        <FormItem label="序号：" prop="codingShowId">
                            <div v-if="drawertitle==1">{{formCustom.codingShowId}}</div>
                            <Input type="text" v-model="formCustom.codingShowId" v-else></Input>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span="24">
                        <FormItem label="说明" prop="note">
                            <div v-if="drawertitle==1">{{formCustom.note}}</div>
                            <Input  v-model="formCustom.note" type="textarea" :autosize="{minRows: 2,maxRows: 5}"  v-else></Input>
                        </FormItem>
                    </Col>
                </Row>
                <FormItem>
                    <Button type="success"  v-show="drawertitle==1" @click='drawershow(2)' style="margin-left: 8px">修改</Button>
                    <Button    v-show="drawertitle==1" @click="datadeleat" style="margin-left: 8px">删除</Button>
                    <Button   @click="handleSubmit('formCustom')" style="margin-left: 8px" v-show="drawertitle==2 || drawertitle==3">提交</Button>
                    <Button @click="handleReset('formCustom')" type="text" style="margin-left: 8px">返回</Button>
                </FormItem>
            </Form>
        </Drawer>
    </div>
</template>

<script>
    import {Api} from '../../../api/index.js'
    export default {
        data() {
            return {
                tage : true,
                codeTree: [],
                value1: false,
                formCustom:{},
                drawertitle:1,
                ruleCustom: {
                    codingName: [
                        { required: true, message: '  ', trigger: 'blur' }
                    ],
                    codingShowId: [
                        { required: true, message: '  ', trigger: 'blur' }
                    ],
                 /*   orgLevel: [
                        { required: true, message: '  ', trigger: 'change' }
                    ],
                    codeAffordIf: [
                        { required: true, message: '  ', trigger: 'blur' }
                    ],*/
                }
            }
        },
        components: {
        },
        created() {
            this.codeTree = [{
                codingSortId : this.$route.params.codingSortId,
                codeName : this.$route.params.codingSortName,
                title : this.$route.params.codingSortName,
                loading: false,
                children: []
            }];
            this.selectNodeInfo = this.codeTree[0];
            this.$http.get(Api.queryCodeDetailBySortId + "/"+ this.selectNodeInfo.codingSortId, {params:{}}).then(res => {
                var nodes=[];
                res.data.data.forEach(item=>{
                    var node = item;
                    node.title = item.codingName;
                    node.superiorCodeId = item.superiorCodeId;
                    node.codingCodeId = item.codingCodeId;
                    node.loading = false;
                    node.children = [];
                    nodes.push(node);
                })
                this.$set(this.selectNodeInfo,'children',nodes);
                //this.$Message.success('成功');
            }).catch(e => {
                this.$Message.error('请求失败');
            })
        },
        methods: {
            treechange(data, item) {
                this.tage = false;
                this.formCustom =item;
                console.log(this.formCustom)
                if(item.codingCodeId) {
                    this.drawershow(1);
                }else{
                    return;
                }
            },
            loadData(selectItem, callback) {
                selectItem.loading = true;
                this.$http.get(Api.queryCodeDetailBySuperiorcode + "/"+ selectItem.codingCodeId, {params:{}}).then(res => {
                    var nodes=[];
                    selectItem.loading = false;
                    if(res.data.data.length !== 0) {
                        res.data.data.forEach(item=>{
                            var node = item;
                            node.title = item.codingName;
                            node.superiorCodeId = item.superiorCodeId;
                            node.codingCodeId = item.codingCodeId;
                            node.loading = false;
                            node.children = [];
                            nodes.push(node);
                        })
                        selectItem.children = nodes;
                        //this.$Message.success('成功');
                    }else{
                        delete selectItem['children'];
                        delete selectItem['loading'];
                    }
                }).catch(e => {
                    this.$Message.error('请求失败');
                })
            },
            drawershow(num){
                if(num==3){
                    //要确定必须传递的参数
                    if((this.formCustom.codingCodeId=='' || this.formCustom.codingCodeId == null || this.formCustom.codingCodeId==undefined) &&
                        (this.formCustom.codingSortId=='' || this.formCustom.codingSortId == null || this.formCustom.codingSortId == undefined)){
                        this.$Message.error({content :'请选择上级编码' , duration: 10});
                        return;
                    }else{
                        let superiorCodeId = this.formCustom.codingCodeId;
                        this.formCustom = {};
                        this.formCustom.superiorCodeId = superiorCodeId ? superiorCodeId : '';
                        this.formCustom.codingSortId = this.$route.params.codingSortId;
                    }
                }
                this.drawertitle = num;
                this.value1 = true;
                if(num==2){
                    this.tage = true;
                }
            },
            datadeleat(){
                this.$Modal.confirm({
                    title: '禁用编码',
                    content: "是否禁用",
                    onOk: () => {
                        this.$http.get(Api.deleteCodeDetail+ '/' +this.formCustom.codingCodeId, {
                            params: {
                            }
                        }).then(res => {
                            this.getCodeTree();
                            this.$Message.info('成功');
                        }).catch(e => {
                            this.$Message.error('请求失败');
                        })
                    },
                    onCancel: () => {
                        this.$Message.info('取消');
                    }
                });
                this.value1 = false
            },

            handleSubmit (name) {
                this.$refs['formCustom'].validate((valid) => {
                    if(valid) {
                        this.$Modal.confirm({
                            title: '保存',
                            content: '<p>确定保存该条信息</p>',
                            okText: '确认',
                            onOk: () => {
                                if(this.tage) {
                                    this.$http.post(Api.updateCodeDetail,  this.formCustom).then(res => {
                                        this.value1 = false
                                        this.$Message.success('成功!');
                                        this.getCodeTree();
                                    }).catch(e => {
                                        this.$Message.error('请求失败');
                                    })
                                } else {
                                    //this.formCustom.orgInfo.orgId = this.formCustom.orgInfo.orgId[0];
                                    this.$http.post(Api.addCodeDetail, this.formCustom).then(res => {
                                        this.value1 = false
                                        this.$Message.success('成功!');
                                        this.getCodeTree();
                                        //this.handleReset()
                                    }).catch(e => {
                                        this.$Message.error('请求失败');
                                    })
                                }
                            },
                            cancelText: '取消'
                        });
                    } else {
                        this.$Message.error('不能为空!');
                    }
                })
            },
            handleReset (name) {
              /*  this.$refs['formCustom'].resetFields();*/
                this.value1 = false
            },
            getCodeTree(){
                this.codeTree = [{
                    codingSortId : this.$route.params.codingSortId,
                    codeName : this.$route.params.codingSortName,
                    title : this.$route.params.codingSortName,
                    loading: false,
                    children: []
                }];
                this.selectNodeInfo = this.codeTree[0];
                this.$http.get(Api.queryCodeDetailBySortId + "/"+ this.selectNodeInfo.codingSortId, {params:{}}).then(res => {
                    var nodes=[];
                    res.data.data.forEach(item=>{
                        var node = item;
                        node.title = item.codingName;
                        node.superiorCodeId = item.superiorCodeId;
                        node.codingCodeId = item.codingCodeId;
                        node.loading = false;
                        node.children = [];
                        nodes.push(node);
                    })
                    this.$set(this.selectNodeInfo,'children',nodes);
                    console.log(this.selectNodeInfo);
                    //this.$Message.success('成功');
                }).catch(e => {
                    this.$Message.error('请求失败');
                })
            },
            routeTo(){
                this.$router.push({name: 'codingSortManager'});
            }
        }

    }
</script>
<style lang='css' scoped>
    @import '../../../index.css';

</style>